<?

class Admin_Posts extends MY_Controller {

    function Admin_Posts() {

        parent::MY_Controller(TRUE);

    }

    function index() {

        $this->mdl_sessions->set_last_index();

        $this->load->view('admin/posts_index');

    }

    function form() {

        if ($this->session->userdata('last_index') == 'admin_index') {

            $this->mdl_sessions->set_last_index('admin_posts/index');

        }

        if ($this->mdl_posts->validate()) {

            $this->mdl_posts->save();

            redirect($this->session->userdata('last_index'));

        }

        else {

            if (!$_POST and $this->uri_assoc('post_id')) {

                $this->mdl_posts->prep_validation($this->mdl_posts->get(array('post_id'=>$this->uri_assoc('post_id'))));

            }

            elseif (!$_POST and !$this->uri_assoc('post_id')) {

                $this->validation->published = 1;

            }

            if ($_POST) {

                $this->validation->date = strtotime($this->input->post('year') . '/' . $this->input->post('month') . '/' . $this->input->post('day') . ' ' . $this->input->post('hour') . ':' . $this->input->post('minute'));

            }

            $this->load->view('admin/posts_form');

        }

    }

    function delete() {

        $this->mdl_posts->delete();

        redirect($this->session->userdata('last_index'));

    }

}

?>